﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;

public partial class DeliverGoodsManagement_PackOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindSenders();
            BindData();
        }

    }
    private void BindData()
    {
        DataSet ds = null;
        string ret = new BLL.ORDERDOC().GetOrderByWhere(" where ORDER_DELETE=0 AND ORDER_CHECK=1 AND ORDER_ACHECK=1 AND ORDER_LOCK=0 AND ORDER_SEND=0 AND (BYZD21 IS NULL OR LEN(BYZD21) =0) order by ORDER_ID DESC", out ds);
        this.Repeater1.DataSource = ds;
        this.Repeater1.DataBind();
    }
    #region 绑定指定发货人
    private void BindSenders()
    {
        List<Model.USERS> senders = new BLL.USERS().GetList("", "USERS_STOP=0","");
       if (senders.Count>0)
        {
            drpSender.DataSource = senders;
            drpSender.DataTextField = "USERS_NAME";
            drpSender.DataValueField = "USERS_CODE";
            drpSender.DataBind();

            ListItem item = new ListItem();
            item.Value = "-1";
            item.Text = "请选择";
            drpSender.Items.Insert(0, item);
        }
    }
    #endregion
    protected void btnSure_Click(object sender, EventArgs e)
    {
        if (drpSender.SelectedValue == "-1")
        {
            Share.Alert(this, "请选择指定发货人"); return;
        }
        string pch = BLL.Common.GetDM("500100");

        StringBuilder sql = new StringBuilder();
        sql.Append("update ORDERDOC set ORDER_SENDER='");
        sql.Append(drpSender.SelectedValue);
        sql.Append("',");
        sql.Append("BYZD21='");
        sql.Append(pch);
        sql.Append("' where ORDER_ID in(");

        int n = 0;
        foreach (RepeaterItem item in Repeater1.Items)
        {
            HtmlInputCheckBox chk = (HtmlInputCheckBox)item.FindControl("chk");
            if (chk.Checked)
            {
                sql.Append("'");
                sql.Append(chk.Value);
                sql.Append("'");
                sql.Append(",");
                n++;
            }
        }
        string str = sql.ToString().Substring(0, sql.ToString().Length - 1);
        str += ")";
        if (n > 0)
        {
            bool result = new SQLServerDAL.ORDERDOC().Deliver(new string[] { str });
            if (!result)
            {
                Share.Alert(this, "打包失败"); return;
            }
            else
            {
                BLL.Common.ADDLSH("500100"); 
                BindData(); 
            }
        }
        else
        { Share.Alert(this, "请选择要打包的订单"); return; }
    }
}
